package com.longcai.controller;


import com.longcai.bean.PageHelper;
import com.longcai.entity.sys.SysRole;
import com.longcai.page.table.PageTableResponse;
import com.longcai.service.SysRoleService;
import com.longcai.vo.SysRoleVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ZJ
 * @since 2019-03-16
 */
@RestController
@RequestMapping("/sysRole/")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping(value = "all")
    @ApiOperation(value = "所有角色")
    @PreAuthorize("hasAnyAuthority('sys:user:query','sys:role:query')")
    public List<SysRole> roles() {
        return this.sysRoleService.sysRoleList();
    }

    @GetMapping(value = "userId")
    @ApiOperation(value = "根据用户id获取拥有的角色")
    @PreAuthorize("hasAnyAuthority('sys:user:query','sys:role:query')")
    public List<SysRole> roles(Long userId) {
        return sysRoleService.listByUserId(userId);
    }

    @ApiOperation(value = "用户列表")
    @GetMapping(value = "rolesList")
    public PageTableResponse rolesList(SysRole sysRole, PageHelper pageHelper) {

        return this.sysRoleService.rolesList(sysRole, pageHelper);
    }

    @GetMapping("{id}")
    @ApiOperation(value = "根据id获取角色")
    @PreAuthorize("hasAuthority('sys:role:query')")
    public SysRole get(@PathVariable Integer id) {
        return this.sysRoleService.selectById(id);
    }

    @PostMapping(value = "saveRole")
    @ApiOperation(value = "保存角色")
    @PreAuthorize("hasAuthority('sys:role:add')")
    public void saveRole(@RequestBody SysRoleVo sysRoleVo) {
        this.sysRoleService.saveRole(sysRoleVo);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除角色")
    @PreAuthorize("hasAuthority('sys:role:del')")
    public void delete(@PathVariable Integer id) {
        this.sysRoleService.deleteSysRole(id);
    }
}